package com.ruoyi.web.controller.system;

import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.CarIoLogDTO;
import com.ruoyi.system.mapper.TzCarIoLogMapper;
import com.ruoyi.web.controller.listener.CarIoLogListener;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TzCarIoLog;
import com.ruoyi.system.service.ITzCarIoLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车牌进出场信息Controller
 * 
 * @author lmj
 * @date 2024-12-07
 */
@Controller
@RequestMapping("/system/tzCarIoLog")
public class TzCarIoLogController extends BaseController
{
    private String prefix = "system/tzCarIoLog";

    @Autowired
    private ITzCarIoLogService tzCarIoLogService;
    @Autowired
    private TzCarIoLogMapper tzCarIoLogMapper;

    @RequiresPermissions("system:tzCarIoLog:view")
    @GetMapping()
    public String tzCarIoLog()
    {
        return prefix + "/tzCarIoLog";
    }

    /**
     * 查询车牌进出场信息列表
     */
    @RequiresPermissions("system:tzCarIoLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TzCarIoLog tzCarIoLog)
    {
        startPage();
        List<TzCarIoLog> list = tzCarIoLogService.selectTzCarIoLogList(tzCarIoLog);
        for(TzCarIoLog a: list){
            a.setSingleTimeStr(DateUtils.singleTimeLabel(a.getSingleTime()));
        }
        return getDataTable(list);
    }

    /**
     * 导出车牌进出场信息列表
     */
    @RequiresPermissions("system:tzCarIoLog:export")
    @Log(title = "车牌进出场信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TzCarIoLog tzCarIoLog)
    {
        List<TzCarIoLog> list = tzCarIoLogService.selectTzCarIoLogList(tzCarIoLog);
        ExcelUtil<TzCarIoLog> util = new ExcelUtil<TzCarIoLog>(TzCarIoLog.class);
        return util.exportExcel(list, "车牌进出场信息数据");
    }

    @RequiresPermissions("system:tzCarIoLog:add")
    @Log(title = "车牌进出场信息", businessType = BusinessType.EXPORT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file)
    {
        try {
            EasyExcel.read(file.getInputStream(), CarIoLogDTO.class, new CarIoLogListener(tzCarIoLogMapper)).sheet().doRead();
        }catch (Exception e){
            logger.info("上传错误！");
        }
        return AjaxResult.success("success");
    }

    /**
     * 新增车牌进出场信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车牌进出场信息
     */
    @RequiresPermissions("system:tzCarIoLog:add")
    @Log(title = "车牌进出场信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TzCarIoLog tzCarIoLog)
    {
        return toAjax(tzCarIoLogService.insertTzCarIoLog(tzCarIoLog));
    }

    /**
     * 修改车牌进出场信息
     */
    @RequiresPermissions("system:tzCarIoLog:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TzCarIoLog tzCarIoLog = tzCarIoLogService.selectTzCarIoLogById(id);
        mmap.put("tzCarIoLog", tzCarIoLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存车牌进出场信息
     */
    @RequiresPermissions("system:tzCarIoLog:edit")
    @Log(title = "车牌进出场信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TzCarIoLog tzCarIoLog)
    {
        return toAjax(tzCarIoLogService.updateTzCarIoLog(tzCarIoLog));
    }

    /**
     * 删除车牌进出场信息
     */
    @RequiresPermissions("system:tzCarIoLog:remove")
    @Log(title = "车牌进出场信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tzCarIoLogService.deleteTzCarIoLogByIds(ids));
    }
}
